package com.example.smart_video.ui.activity;

import static com.example.smart_video.adapter.HBomeListAdapter.SCENE_SELECT_CLASS_LIST;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.example.cleandemo3.R;
import com.example.smart_video.adapter.HBomeListAdapter;
import com.example.cleandemo3.databinding.ActivitySelectClassBinding;
import com.example.smart_video.model.Constants;
import com.example.smart_video.model.json.CommonRootBean;
import com.example.smart_video.model.json.VideoClassData;
import com.example.smart_video.utils.CommonUtils;
import com.wink_172.library.AppManager;
import com.wink_172.library.activity.SmartActivity;
import com.wink_172.library.model.BaseDataItem;
import com.wink_172.library.view.ActionBarView;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;
import java.util.SortedMap;

/**
 * 选择分类
 */
public class SelectClassActivity extends SmartActivity {

    public ActivitySelectClassBinding binding;

    public static void startActivity(Activity context, Object... args) {
        Intent intent = new Intent(context, SelectClassActivity.class);

//        intent.putExtra(Constants.PARAM_DATA1, (String) args[0]);
        context.startActivityForResult(intent, (Integer) args[0]);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = DataBindingUtil.setContentView(this, R.layout.activity_select_class);

        initView();
        getClassList();

    }

    public void initView() {

        ((ActionBarView) binding.actionBar).updateAllContent(R.mipmap.icon_nav_arrow, "选择分类", "");
        ((ActionBarView) binding.actionBar).setCallback(new ActionBarView.ICallback() {
            @Override
            public void onSendEvent(int event, String content) {
                if (event == ActionBarView.LEFT_BTN) {
                    AppManager.Companion.getAppManager().finishActivity();

                }
            }
        });


        List<Object> list = new ArrayList();
        HBomeListAdapter homeListAdapter = new HBomeListAdapter(this, list, binding.listView);
        binding.listView.init(homeListAdapter, false);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(AppManager.Companion.getAppManager().currentActivity(), 4, GridLayoutManager.VERTICAL, false);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
//                if (position < 7 || position > 14) {
//                    return 3;
//                }
                return 2;
            }
        });
        binding.listView.setLayoutManager(gridLayoutManager);

        homeListAdapter.setMOnItemClickListener(new HBomeListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                HBomeListAdapter homeListAdapter = (HBomeListAdapter) binding.listView.getAdapter();
                List<Object> list = homeListAdapter.getDataList();
                BaseDataItem baseDataItem = (BaseDataItem) list.get(position);

                VideoClassData classData = (VideoClassData) baseDataItem.getData();

                Intent intent = new Intent();
                intent.putExtra(com.wink_172.library.model.Constants.PARAM_DATA1, classData);
                setResult(Activity.RESULT_OK, intent);
                finish();
            }
        });
        binding.listView.setLayoutManager(gridLayoutManager);

    }

    private static final String TAG = "SelectClassActivity";


    /**
     * 获取分类列表
     */
    private void getClassList() {
        String url = Constants.BASE_URL + "video_action/classlist";
        SortedMap parameters = CommonUtils.getBaseSortedMap();
        parameters.put("root_mode", "" + Constants.ROOT_MODE);
        RequestParams params = CommonUtils.getTargetRequest(url, parameters);


        Callback.CommonCallback commonCallback = new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                CommonRootBean commonRootBean = JSON.parseObject(result, CommonRootBean.class);

                List<VideoClassData> classList = JSON.parseArray(commonRootBean.getData(), VideoClassData.class);
                initList(classList);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {


            }
        };

        x.http().get(params, commonCallback);
    }

    public void initList(List<VideoClassData> classList) {
        List<Object> list = new ArrayList<Object>();


        for (int i = 0; i < classList.size(); i++) {
            VideoClassData videoClassData = classList.get(i);
            BaseDataItem baseDataItem = new BaseDataItem(i, SCENE_SELECT_CLASS_LIST);
            baseDataItem.setObjectData(videoClassData);

            list.add(baseDataItem);


        }

        BaseDataItem baseDataItem = new BaseDataItem(-1, SCENE_SELECT_CLASS_LIST);
        VideoClassData videoClassData = new VideoClassData();
        videoClassData.setId(-1);
        videoClassData.setName("清除分类-1");
        baseDataItem.setObjectData(videoClassData);
        list.add(baseDataItem);


        HBomeListAdapter homeListAdapter = (HBomeListAdapter) binding.listView.getAdapter();
        homeListAdapter.swapContent(list);
        homeListAdapter.notifyDataSetChanged();
    }
}